package alexattar.gdxtest;

import alexattar.gdxtest.screens.LevelScreen;
import alexattar.gdxtest.screens.SplashScreen;
import alexattar.gdxtest.services.LevelManager;
import alexattar.gdxtest.services.MusicManager;
import alexattar.gdxtest.services.PreferencesManager;
import alexattar.gdxtest.services.ProfileManager;
import alexattar.gdxtest.services.SoundManager;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GL10;

public class GdxTestGame extends Game {
	
	public static final String LOG = GdxTestGame.class.getSimpleName();
	
	public static final boolean DEV_MODE = true;
	
	private FPSLogger fpsLogger;
	
	private ProfileManager profileManager;
	private PreferencesManager preferencesManager; 
	private MusicManager musicManager;
	private SoundManager soundManager;
	private LevelManager levelManager;
	
	public GdxTestGame() {
	}
	
	public ProfileManager getProfileManager() {
		return profileManager;
	}
	
	public PreferencesManager getPreferences() {
		return preferencesManager;
	}
	
	public MusicManager getMusicManager() {
		return musicManager;
	}
	
	public SoundManager getSoundManager() {
		return soundManager;
	}
	
	public LevelManager getLevelManager() {
		return levelManager;
	}
	
	@Override
	public void create() {		
		Gdx.app.log(GdxTestGame.LOG, "Creating Game");

		preferencesManager = new PreferencesManager();
		
		musicManager = new MusicManager();
		musicManager.setVolume(preferencesManager.getVolume());
		musicManager.setEnabled(preferencesManager.isMusicEnabled());
		
		soundManager = new SoundManager();
		soundManager.setVolume(preferencesManager.getVolume());
		soundManager.setEnabled(preferencesManager.isSoundEffectsEnabled());
		
		profileManager = new ProfileManager();
		profileManager.retrieveProfile();
		
		levelManager = new LevelManager();
		
		fpsLogger = new FPSLogger();
	}

	@Override
	public void render() {		
		super.render();
		
		if (DEV_MODE) fpsLogger.log();
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
		Gdx.app.log(GdxTestGame.LOG, "Resizing game to " + width + "x" + height);
		if (getScreen() == null) {
			if (DEV_MODE) {
				setScreen(new LevelScreen(this, 0));
			}
			else {
				setScreen(new SplashScreen(this));
			}
		}
	}

	@Override
	public void pause() {
		super.pause();
		Gdx.app.log(GdxTestGame.LOG, "Pausing game");
		profileManager.persist();
	}

	@Override
	public void resume() {
		super.resume();
		Gdx.app.log(GdxTestGame.LOG, "Resuming game");
	}
	
	@Override 
	public void setScreen(Screen screen) {
		super.setScreen(screen);
		Gdx.app.log(GdxTestGame.LOG, "Setting screen: " + screen.getClass().getSimpleName());
	}

	@Override
	public void dispose() {
		super.dispose();
		Gdx.app.log(GdxTestGame.LOG, "Disposing game");
		musicManager.dispose();
		soundManager.dispose();
	}
}
